//
//  ZCEmoticonTools.swift
//  weibo
//
//  Created by Ning Xie on 16/8/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let NumberPage = 20
class ZCEmoticonTools: NSObject {

    
    static let sharedZCEmoticonTools: ZCEmoticonTools = ZCEmoticonTools()
    
    private override init() {
        super.init()
    }
    
    
    //创建bundle文件
    private lazy var emoticonBundle:NSBundle = {
    
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        
        let bundle = NSBundle(path: path!)!
        
        return bundle
    }()
    
    //默认表情
    private lazy var defaultEmoticonArray:[ZCEmoticonModel] = {
      
        let tempArray = self.loadEmoticonArrayWithSubPath("default/info.plist")
        return tempArray
        
    }()
    //emoji表情
    private lazy var emojiEmoticonArray:[ZCEmoticonModel] = {
       
        let tempArray = self.loadEmoticonArrayWithSubPath("emoji/info.plist")
        return tempArray
    }()
    //浪小花表情
    private lazy var lxhEmoticonArray:[ZCEmoticonModel] = {
       
        let tempArray = self.loadEmoticonArrayWithSubPath("lxh/info.plist")
        return tempArray
    }()
    
    //用于加载表情数组的方法
    private func loadEmoticonArrayWithSubPath(subPath:String)->[ZCEmoticonModel]{
        
        let path = self.emoticonBundle.pathForResource(subPath, ofType: nil)
        
        let array = NSArray(contentsOfFile: path!)!
        
        var tempArray = [ZCEmoticonModel]()
        for dic in array {
            
            let emoticon = ZCEmoticonModel(dic: dic as! [String : AnyObject])
            if emoticon.type == "0" {
                let result = (path! as NSString).stringByDeletingLastPathComponent
                
                emoticon.path = result + "/" + emoticon.png!
            }
            tempArray.append(emoticon)
        }
        return tempArray
    }
    
    //根据表情数组拆分为而为数组
    private func sectionEmoticonWithEmoticonArray(emoticon:[ZCEmoticonModel])-> [[ZCEmoticonModel]]{
        
        let pageCount = (emoticon.count - 1) / NumberPage + 1
    
        var tempArray = [[ZCEmoticonModel]]()
        for page in 0..<pageCount{
            
            let location = page * NumberPage
            var  length = NumberPage
            
            if location + length > emoticon.count {
                length = emoticon.count - location
            }
            
            let range = NSMakeRange(location, length)
            
            let subArray = (emoticon as NSArray).subarrayWithRange(range) as! [ZCEmoticonModel]
            
            tempArray.append(subArray)
        }
        return tempArray
        
    }
    
    //给表情视图提供的数据
    lazy var allEmoticon:[[[ZCEmoticonModel]]] = {
        
        let defaultArray = self.sectionEmoticonWithEmoticonArray(self.defaultEmoticonArray)
        let emojiArray = self.sectionEmoticonWithEmoticonArray(self.emojiEmoticonArray)
        let lxhArray = self.sectionEmoticonWithEmoticonArray(self.lxhEmoticonArray)
        
        let array = [defaultArray,emojiArray,lxhArray]
        return array
    }()
    
    func searchEmoticonWithChs(chs: String) -> ZCEmoticonModel?{
        
        for value in defaultEmoticonArray {
            if value.chs == chs {
                return value
            }
        }
        for value in lxhEmoticonArray {
            if value.chs == chs {
                return value
            }
        }
        return nil
    }
}
